package com.example.b_308.myapplication.view.register;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.controller.PhoneValidator;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.view.base.BaseFragment;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.service.IUserView;

/**
 * Created by B-308 on 2017/11/30.
 */
public class RegisterFragment1 extends BaseFragment implements View.OnClickListener, IUserView {
    UserController userController = new UserController(this);
    EditText register_phone, register_code;
    Button btn_next,btn_getcode;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_register1, null);

        register_phone = (EditText) view.findViewById(R.id.register_phone);
        register_code = (EditText) view.findViewById(R.id.register_code);
        btn_next= (Button) view.findViewById(R.id.btn_next);
        btn_getcode= (Button) view.findViewById(R.id.btn_getcode);
        btn_getcode.setOnClickListener(this);
        register_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (register_code.getText()!=null&&register_code.length()==4){
                    btn_next.setSelected(true);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        view.findViewById(R.id.btn_next).setOnClickListener(this);

        return view;
    }


    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void getCodeSuccess() {
    }

    @Override
    public void onFail(String message) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_getcode:
                btn_getcode.setBackgroundColor(Color.parseColor("#B6B6B6"));
                runOnBackground(new Runnable() {
                    @Override
                    public void run() {
                    }
                });
                break;
//                userController.getCode(register_phone.getText().toString());
            case R.id.btn_next:
                if (PhoneValidator.isPhoneNumber(register_phone.getText().toString())) {
                    if (!register_code.getText().toString().isEmpty()) {
                        ((RegisterActivity) getActivity()).next(this);
//                        , register_phone.getText().toString()
                    } else {
                        ((RegisterActivity) getActivity()).onFail("验证码错误");
                    }
                } else {
                    ((RegisterActivity) getActivity()).onFail("手机号码格式错误");
                }
                break;
        }

    }
    public void runOnBackground(Runnable runnable) {
        new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                new Thread() {
                    int timer = 60;

                    @Override
                    public void run() {
                        super.run();
                        while (timer >= 0) {
                            timer -= 1;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (timer > 0) {
                                        btn_getcode.setText("" + timer + "秒");
                                        btn_getcode.setClickable(false);
                                        btn_getcode.setSelected(true);
                                    } else {
                                        btn_getcode.setBackgroundColor(Color.parseColor("#F5A332"));
                                        btn_getcode.setText("重新获取");
                                        btn_getcode.setClickable(true);
                                        btn_getcode.setSelected(false);
                                    }
                                }
                            });
                        }
                    }
                }.run();

                return null;
            }
        }.execute();
    }


}